package com.valuedaas.schoolsafety.service.recources;

import com.mongodb.client.model.Filters;
import com.valuedaas.schoolsafety.common.base.Pagination;
import com.valuedaas.schoolsafety.common.collects.sys.SysDic;
import com.valuedaas.schoolsafety.service.BaseService;
import org.apache.commons.collections.CollectionUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DicService extends BaseService {

	@Override
	protected String collName() {
		return SysDic.COLL_NAME;
	}

	/**
	 * 根据字典key，获取字典信息
	 * 
	 * @param dicKey
	 * @return
	 */
	public Document getBydicKey(String dicKey) {
		Document doc = mongoTemplate.getCollection(SysDic.COLL_NAME).find(new Document(SysDic.DICKEY, dicKey))
				.first();
		return doc;
	}

	/**
	 * 向数据库添加字典值
	 * 
	 * @param data
	 */
	public void addDic(Document data) {
		mongoTemplate.getCollection(SysDic.COLL_NAME).insertOne(data);
	}

	/**
	 * 删除字典值
	 * 
	 * @param id
	 *            主键
	 * @return boolean
	 */
	public boolean deleteDic(String id) {
		long count = mongoTemplate.getCollection(SysDic.COLL_NAME).deleteOne(new Document("_id", new ObjectId(id)))
				.getDeletedCount();
		return count > 0;
	}

	/**
	 * 修改字典值
	 * 
	 * @param id
	 *            主键
	 * @param document
	 *            数据
	 * @return boolean
	 */
	public boolean updateDic(String id, Document document) {
		long count = mongoTemplate.getCollection(SysDic.COLL_NAME)
				.updateOne(Filters.eq("_id", new ObjectId(id)), new Document("$set", document)).getMatchedCount();
		return count > 0;
	}

	/**
	 * 获取字典列表
	 * 
	 * @param pageNo
	 *            页面
	 * @param pageSize
	 *            每页多少条
	 * @return map
	 */
	public Map<String, Object> listDic(Bson filter, Integer pageNo, Integer pageSize) {
		Long total = mongoTemplate.getCollection(SysDic.COLL_NAME).count(filter);
		Pagination page = new Pagination(pageNo, pageSize, total.intValue());

		List<Document> docList = new ArrayList<Document>();
		int skip = (pageNo - 1) * pageSize;
		mongoTemplate.getCollection(SysDic.COLL_NAME).find(filter).skip(skip).limit(pageSize).into(docList);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", docList);
		map.put("page", page);

		return map;
	}

	/**
	 * 检查字典是否 已经存在
	 * 
	 * @param dicKey
	 * @return
	 */
	public boolean checkExist(String dicKey) {
		return mongoTemplate.getCollection(SysDic.COLL_NAME).count(Filters.eq(SysDic.DICKEY, dicKey)) > 0;
	}

	/**
	 * 检查字典是否 已经存在
	 * 
	 * @param dicKey
	 * @param id
	 * @return
	 */
	public boolean checkExistForUpdate(String dicKey, String id) {
		Document doc = mongoTemplate.getCollection(SysDic.COLL_NAME).find(Filters.eq(SysDic.DICKEY, dicKey)).first();
		if (doc != null && !id.equals(doc.getObjectId(SysDic._ID).toString())) {
			return true;
		}
		return false;
	}

	/**
	 * 根据字典key，获取所有childs， 返回 map形式
	 * @param dicKey
	 * @return
	 */
	public Map<String, String> getChildMapBydicKey(String dicKey) {
		Document doc = this.getBydicKey(dicKey);
		Map<String, String> childMap = new HashMap<>();
		if (doc != null) {
			@SuppressWarnings("unchecked")
			List<Document> childs = (List<Document>) doc.get(SysDic.CHILDS);
			if (CollectionUtils.isNotEmpty(childs)) {
				childs.forEach((c) -> {
					childMap.put(c.getString(SysDic.KEY), c.getString(SysDic.VAL));
				});
			}
		}
		return childMap;
	}

	/**
	 * 根据字典key，获取所有childs， 返回 map形式 
	 * @param dicKey
	 * @return
	 */
	public Map<String, String> getChildReverseMapBydicKey(String dicKey) {
		Document doc = this.getBydicKey(dicKey);
		Map<String, String> childMap = new HashMap<>();
		if (doc != null) {
			@SuppressWarnings("unchecked")
			List<Document> childs = (List<Document>) doc.get(SysDic.CHILDS);
			if (CollectionUtils.isNotEmpty(childs)) {
				childs.forEach((c) -> {
					childMap.put(c.getString(SysDic.VAL), c.getString(SysDic.KEY));
				});
			}
		}
		return childMap;
	}
}
